<template>
  <div class="main_box" style="padding:10px;">
    <div class="add_wrap">
      <div class="tit">
        <h2>医院信息</h2>
        <el-button
          type="success"
          size="small"
          plain
          icon="fa fa-mail-forward"
          @click="returnList"
          >返回</el-button
        >
      </div>
      <el-form
        :model="reportForm"
        ref="reportForm"
        label-width="200px"
        :rules="roleRules"
      >
        <el-row>
          <el-form-item label="医院名称" prop="hospitalId">
            <el-select
              v-model="reportForm.hospitalId"
              filterable
              clearable
              reserve-keyword
              :loading="loadingIns"
              placeholder="请选择医院"
              @change="hospitalChange"
            >
              <el-option
                v-for="item in pickHospitalList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="科室名称" prop="officeId">
            <el-select
              v-model="reportForm.officeId"
              filterable
              clearable
              reserve-keyword
              :loading="loadingIns"
              placeholder="请选择科室"
              @change="officeChange"
            >
              <el-option
                v-for="item in pickOfficeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <a
              href="javascript:void(0);"
              @click="addOffice"
              style="margin-left: 230px"
              >快速创建</a
            >
          </el-form-item>

          <el-form-item label="医生名称" prop="doctorId">
            <el-select
              v-model="reportForm.doctorId"
              filterable
              clearable
              reserve-keyword
              :loading="loadingIns"
              placeholder="请选择医生"
            >
              <el-option
                v-for="item in pickDoctorList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <a
              href="javascript:void(0);"
              @click="addDoctor"
              style="margin-left: 230px"
              >快速创建</a
            >
          </el-form-item>
        </el-row>


        <div class="tit" v-if="userRole === 1">
          <h2>项目信息</h2>
        </div>
        <el-row>
           <el-form-item label="项目名称" prop="itemName">
            <el-input v-model="reportForm.itemName"></el-input>
          </el-form-item>
        </el-row>
        <div class="tit" v-if="userRole === 1">
          <h2>医检机构</h2>
        </div>
        <el-row v-if="userRole === 1">
          <el-form-item label="医检名称" prop="institutionId">
            <el-select
              v-model="reportForm.institutionId"
              clearable
              filterable
              reserve-keyword
              :loading="loadingIns"
              placeholder="请选择医检机构"
              @focus="listInstitution"
              @change="institutionChange"
            >
              <el-option
                v-for="item in pickInstitutionList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="科室名称" prop="insOfficeId">
            <el-select
              v-model="reportForm.insOfficeId"
              clearable
              filterable
              reserve-keyword
              :loading="loadingIns"
              placeholder="请选择科室"
            >
              <el-option
                v-for="item in pickInsOfficeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <!--<el-form-item label="报告状态" prop="reportState">
          <el-select
            v-model="reportForm.reportState"
            clearable
            reserve-keyword
            placeholder="请选择当前报告状态">
            <el-option
              v-for="item in pickCurReportStateList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>-->
        </el-row>

        <div class="tit">
          <h2>基础信息</h2>
        </div>
        <el-row>
          <el-form-item label="医检条码号" prop="insReportCode">
            <el-input
              v-model="reportForm.insReportCode"
              @blur="checkInsCodeIsExist"
            ></el-input>
            <br /><span
              id="insCodeHides"
              :hidden="insCodeHide"
              style="color: red; font-size: xx-small; height: 0px;"
            >
              <i class="el-icon-warning" />医检条码号重复！</span
            >
          </el-form-item>
          <el-form-item
            label="平台条码号"
            prop="myReportCode"
            :disabled="disabledMyReportCode"
          >
            <el-input v-model="reportForm.myReportCode"></el-input>
          </el-form-item>
          <el-form-item label="查询号" prop="searchCode" v-if="false">
            <el-input v-model="reportForm.searchCode"></el-input>
          </el-form-item>
          <el-form-item label="送检时间" prop="reportBeginTime">
            <el-date-picker
              v-model="reportForm.reportBeginTime"
              type="datetime"
              :default-value="timeDefaultShow"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="不填则默认为当前时间"
            ></el-date-picker>
          </el-form-item>
           <el-form-item label="一级收样员" prop="syUserId">
            <el-select
              v-model="reportForm.syUserId"
              :disabled="syUserIdState"
              filterable
              reserve-keyword
              placeholder="请选择一级收样员"
            >
              <el-option
                v-for="item in pickSyUserList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item id="groupSyUser" label="二级收样员" prop="syUserIdTwo">
            <el-select
              v-model="reportForm.syUserIdTwo"
              :disabled="syUserIdTwoState"
              filterable
              reserve-keyword
              placeholder="请选择二级收样员"
            >
              <el-option
                v-for="item in pickSyUserList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <el-button
              :disabled="syUserEnable"
              @click="addSubclass()"
              icon="el-icon-plus"
              plain
              round
              type="success"
              size="mini"
            ></el-button>
          </el-form-item>

          <!-- 5.21 liusixiang 次级收样员 -->
          <el-form-item
            prop="syUserIdList"
            id="groupSyUser"
            v-for="(item, index) in reportForm.syUserIdList"
            :label="changSyUserLabel(index + 3)"
            :key="'syUserIdList' + index"
          >
            <el-select
              v-model.lazy="item.userId"
              :disabled="item.collectState > 0"
              filterable
              reserve-keyword
              placeholder="请选择收样员"
            >
              <el-option
                v-for="item in pickSyUserList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <el-button
              :disabled="syUserEnable"
              @click="deleteSubclass(index)"
              icon="el-icon-close"
              plain
              round
              type="danger"
              size="mini"
            ></el-button>
          </el-form-item>
          <!-- v-if="index == 0" --给添加符号设置限制条件 -->
          <el-form-item label="是否需要结果提醒" prop="isRemind">
            <el-switch
              v-model="reportForm.isRemind"
              active-color="#13ce66"
              active-value="1"
              inactive-value="0"
            ></el-switch>
          </el-form-item>
        </el-row>

        <div class="tit">
          <h2>患者信息</h2>
        </div>
        <el-row>
          <el-form-item label="患者姓名" prop="patientName">
            <el-input v-model="reportForm.patientName"></el-input>
          </el-form-item>
          <el-form-item label="患者性别" prop="patientSex">
            <el-radio-group v-model="reportForm.patientSex">
              <el-radio :label="1">男</el-radio>
              <el-radio :label="2">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="患者身份证" prop="patientCard">
            <el-input v-model="reportForm.patientCard"></el-input>
          </el-form-item>
          <el-form-item label="证件类型" prop="cardType">
            <el-radio-group v-model="reportForm.cardType">
              <el-radio :label="1">居民身份证</el-radio>
              <el-radio :label="9">其他</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="患者年龄" prop="patientAge">
            <el-input v-model="reportForm.patientAge"></el-input>
          </el-form-item>
          <el-form-item label="患者电话" prop="patientPhone">
            <el-input v-model="reportForm.patientPhone"></el-input>
          </el-form-item>
          <el-form-item label="基本病情" prop="patientInfo">
            <el-input
              type="textarea"
              v-model="reportForm.patientInfo"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="reportForm.remark"></el-input>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item>
            <el-button
              type="primary"
              @click="sureReportClick"
              :disabled="disabledBtn"
              >保 存</el-button
            >
            <el-button @click="returnList" :disabled="disabledBtn"
              >取 消</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
    </div>

    <el-dialog
      title="新增科室"
      :visible.sync="dialogOfficeFormVisible"
      :center="isCenter"
      width="500px"
    >
      <el-form :model="addOfficeForm">
        <el-form-item label="科室名称：" label-width="150px">
          <el-input
            v-model="addOfficeForm.officeName"
            autocomplete="off"
            style="width: 250px"
          ></el-input>
        </el-form-item>
        <el-form-item align="center">
          <el-button @click.native="dismissOffice">取 消</el-button>
          <el-button type="primary" @click.native="commitOffice"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog
      title="新增医生"
      :visible.sync="dialogDoctorFormVisible"
      :center="isCenter"
      width="500px"
    >
      <el-form :model="addDoctorForm">
        <el-form-item label="医生姓名：" label-width="150px">
          <el-input
            v-model="addDoctorForm.doctorName"
            autocomplete="off"
            style="width: 250px"
          ></el-input>
        </el-form-item>
        <el-form-item label="医生职称：" label-width="150px">
          <el-input
            v-model="addDoctorForm.doctorTitle"
            autocomplete="off"
            style="width: 250px"
          ></el-input>
        </el-form-item>
        <el-form-item align="center">
          <el-button @click.native="dismissDoctor">取 消</el-button>
          <el-button type="primary" @click.native="commitDoctor"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>

    <sameReportCheck
      ref="sameReportCheck"
      :dataList="sameReportList"
      @addReportSure="addReportSure"
    ></sameReportCheck>
  </div>
</template>

<script>
import * as message from "@/constants/message";
import SameReportCheck from "@/components/SameReportCheck";
import ReportApi from "@/api/hsReport/hsReport";
import OfficeApi from "@/api/institution/office";
import HospitalApi from "@/api/institution/hospital";
import DoctorApi from "@/api/institution/doctor";
import { mapGetters, mapActions } from "vuex";
import InstitutionApi from "@/api/institution/institution";
import AuthApi from "@/api/system/auth";
import { getStorage } from "@/utils/auth";
import * as stn from "@/constants/storeName";

export default {
  data() {
    return {
      hosItemId:"",//判断是否有医院项目组
      loadingIns: false,
      loadingCla: false,
      loadingUpload: false,
      pickHospitalList: [],
      pickOfficeList: [],
      pickDoctorList: [],
      pickInstitutionList: [],
      pickInsOfficeList: [],
      pickSyUserList: [],
      reportImageList: [],
      sameReportList: [], // 相同平台/医检条码号的旧报告单列表
      disabled: false,
      disabledBtn: false,
      disabledMyReportCode: false,
      isCenter: true,
      fullscreenLoading: false,
      userRole: "",
      dialogImageUrl: "",
      dialogVisible: false,
      insCodeHide: true,
      dialogOfficeFormVisible: false,
      dialogDoctorFormVisible: false,
      addOfficeForm: {
        hospitalId: "",
        officeName: ""
      },
      addDoctorForm: {
        hospitalId: "",
        officeId: "",
        doctorName: "",
        doctorTitle: ""
      },
      oldReportInfo: {
        codeType: "",
        myReportCode: "",
        insReportCode: "",
        institutionId: ""
      },
      syUserEnable: false,
      syUserIdState: false,
      syUserIdTwoState: false,
      reportForm: {
        reportId: "",
        codeFlag: "",
        myReportCode: "",
        searchCode: "",
        patientName: "",
        patientSex: "",
        patientAge: "",
        patientPhone: "",
        cardType: "",
        patientCard: "",
        patientInfo: "",
        itemName: "",
        isRemind: "0",
        hospitalId: "",
        officeId: "",
        doctorId: "",
        institutionId: "",
        insOfficeId: "",
        insReportCode: "",
        syUserId: "",
        syUserIdTwo: "", //二级收样员
        syUserIdList: [], // 三级及其后续收样员
        //reportState: '',
        remark: "",
        reportImageList: []
      },
      pickerBeginDateBefore: {
        disabledDate(time) {
          return time.getTime() < Date.now();
        }
      },
      timeDefaultShow: "",
      roleRules: {
        reportCode: [
          {
            required: true,
            message: "请输入条码号",
            trigger: "blur"
          }
        ],
        itemName: [
          {
            required: true,
            message: "请输入送检项目",
            trigger: "blur"
          }
        ],
        patientName: [
          {
            required: true,
            message: "请输入患者姓名",
            trigger: "blur"
          }
        ],
        hospitalId: [
          {
            required: true,
            message: "请选择医院",
            trigger: "change"
          }
        ],
        cardType: [
          {
            required: true,
            message: "请选择证件类型",
            trigger: "change"
          }
        ],
        patientAge: [
          {
            pattern: /^([1-9]|[1-9]\d|1\d{2}|200)$/,
            message: "输入格式不正确",
            trigger: "blur"
          }
        ],
        patientPhone: [
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "电话号码格式异常",
            trigger: "blur"
          }
        ]
      }
    };
  },
  components: {
    SameReportCheck
  },
  beforeRouteLeave(to, from, next) {
    to.meta.keepAlive = true;
    next();
  },
  methods: {
    ...mapGetters({
      openTabs: "getOpenTabs"
    }),
    ...mapActions({
      setOpenTabs: "setOpenTabs",
      setActiveIndexPath: "setActiveIndexPath",
      setDeleteTabs: "setDeleteTabs",
      replaceOpenTab: "replaceOpenTab"
    }),
    init() {
      if (this.$route.query.id) {
        this.getData();
        this.userInfo = getStorage(stn.USER_INFO);
        if (this.userInfo) {
          this.userRole = this.userInfo.userRole;
        }
      }
    },
    checkInsCodeIsExist() {
      let _this = this;
      _this.insCodeHide = true;
      if (!_this.reportForm.institutionId) {
        this.$message.error("请选择医检机构");
        return;
      }
      let cond = {
        institutionId: _this.reportForm.institutionId,
        insReportCode: _this.reportForm.insReportCode
      };
      ReportApi.getReportCount(cond)
        .then(res => {
          if (res.data && res.data > 0) {
            _this.insCodeHide = false;
          }
        })
        .catch(error => {
          _this.insCodeHide = true;
          console.error(error);
        });
    },
    listInstitution() {
      let _this = this;
      _this.loadingIns = true;
      let cond = {
        hospitalId: this.reportForm.hospitalId
      };
      InstitutionApi.getInsByHos(cond)
        .then(res => {
          setTimeout(() => {
            _this.loadingIns = false;
            _this.pickInstitutionList = res.data.map(item => {
              return {
                value: item.institutionId,
                label: item.institutionName
              };
            });
          }, 200);
        })
        .catch(error => {
          _this.pickInstitutionList = [];
          console.error(error);
        });
    },
    institutionChange() {
      let _this = this;
      _this.pickInsOfficeList = [];
      _this.reportForm.insOfficeId = "";
      let cond = {
        institutionId: _this.reportForm.institutionId
      };
      OfficeApi.getOfficeNames(cond).then(res => {
        setTimeout(() => {
          _this.pickInsOfficeList = res.data.map(item => {
            return {
              value: item.officeId,
              label: item.officeName
            };
          });
        }, 200);
      });
    },
    listInsOffice(){
       let cond = {
        institutionId: this.reportForm.institutionId
      };
      OfficeApi.getOfficeNames(cond).then(res => {
        setTimeout(() => {
          this.pickInsOfficeList = res.data.map(item => {
            return {
              value: item.officeId,
              label: item.officeName
            };
          });
        }, 200);
      });
    },
    listHospital() {
      let _this = this;
      _this.loadingIns = true;
      let cond = {
        hospitalType: 1
      };
      HospitalApi.getHospitalNameList(cond).then(res => {
        setTimeout(() => {
          _this.loadingIns = false;
          _this.pickHospitalList = res.data.map(item => {
            return {
              value: item.hospitalId,
              label: item.hospitalName
            };
          });
        }, 200);
      });
    },
    listOffice() {
      let _this = this;
      let cond = {
        hospitalId: _this.reportForm.hospitalId
      };
      OfficeApi.getOfficeNames(cond)
        .then(res => {
          setTimeout(() => {
            _this.pickOfficeList = res.data.map(item => {
              return {
                value: item.officeId,
                label: item.officeName
              };
            });
          }, 200);
        })
        .catch(error => {
          _this.pickOfficeList = [];
          console.error(error);
        });
    },
    listDoctor() {
      let _this = this;
      let cond = {
        hospitalId: _this.reportForm.hospitalId,
        officeId: _this.reportForm.officeId
      };
      DoctorApi.getDoctorNames(cond)
        .then(res => {
          setTimeout(() => {
            _this.pickDoctorList = res.data.map(item => {
              return {
                value: item.doctorId,
                label: item.doctorName
              };
            });
          }, 200);
        })
        .catch(error => {
          _this.pickDoctorList = [];
          console.error(error);
        });
    },
    hospitalChange() {
      let _this = this;
      if (
        _this.reportForm.hospitalId == "" ||
        _this.reportForm.hospitalId == null
      ) {
        this.$notify.error({
          title: message.TITLE_PROMPT,
          message: "请先选择医院"
        });
        return;
      }
      _this.pickOfficeList = [];
      _this.reportForm.officeId = "";
      _this.pickDoctorList = [];
      _this.reportForm.doctorId = "";
      _this.pickInstitutionList = [];
      _this.reportForm.institutionId = "";
      let cond = {
        hospitalId: _this.reportForm.hospitalId
      };
      OfficeApi.getOfficeNames(cond)
        .then(res => {
          setTimeout(() => {
            _this.pickOfficeList = res.data.map(item => {
              return {
                value: item.officeId,
                label: item.officeName
              };
            });
          }, 200);
        })
        .catch(error => {
          _this.pickOfficeList = [];
          console.error(error);
        });
    },
    officeChange() {
      let _this = this;
      _this.pickDoctorList = [];
      _this.reportForm.doctorId = "";
      if (
        _this.reportForm.hospitalId == "" ||
        _this.reportForm.hospitalId == null
      ) {
        this.$notify.error({
          title: message.TITLE_PROMPT,
          message: "请先选择医院"
        });
        return;
      }
      if (
        _this.reportForm.officeId == "" ||
        _this.reportForm.officeId == null
      ) {
        this.$notify.error({
          title: message.TITLE_PROMPT,
          message: "请先选择科室"
        });
        return;
      }
      
      let cond = {
        hospitalId: _this.reportForm.hospitalId,
        officeId: _this.reportForm.officeId
      };
      DoctorApi.getDoctorNames(cond)
        .then(res => {
          setTimeout(() => {
            _this.pickDoctorList = res.data.map(item => {
              return {
                value: item.doctorId,
                label: item.doctorName
              };
            });
          }, 200);
        })
        .catch(error => {
          _this.pickDoctorList = [];
          console.error(error);
        });
    },
    listSyUser() {
      let _this = this;
      AuthApi.listAdminByRole({
        roleId: 4
      }).then(res => {
        setTimeout(() => {
          _this.pickSyUserList = res.data.map(item => {
            return {
              value: item.userId,
              label: item.userName
            };
          });
        }, 200);
      });
    },
    // 无限增加次级收样员
    addSubclass() {
      if (!this.reportForm.syUserId) {
        this.$message.error("请选择一级收样员");
        return;
      }
      if (!this.reportForm.syUserIdTwo) {
        this.$message.error("请选择二级收样员");
        return;
      }
      let data = { userId: null };
      this.reportForm.syUserIdList.push(data);
      //console.log(JSON.stringify(this.reportForm.syUserIdList),333333333)
    },
    // 删除次级收样员
    deleteSubclass(index) {
      this.reportForm.syUserIdList.splice(index, 1);
      //console.log(JSON.stringify(this.reportForm.syUserIdList),1111111111)
    },
    // 改变多级收样员label
    changSyUserLabel(index) {
      let result = this.$Utils.numberToChinese(index);
      return result + "级收样员";
    },
    addOffice() {
      let _this = this;
      if (
        _this.reportForm.hospitalId == "" ||
        _this.reportForm.hospitalId == null
      ) {
        this.$notify.error({
          title: message.TITLE_PROMPT,
          message: "请先选择医院"
        });
        return;
      }
      this.dialogOfficeFormVisible = true;
      _this.addOfficeForm.hospitalId = _this.reportForm.hospitalId;
    },
    dismissOffice() {
      this.dialogOfficeFormVisible = false;
      this.addOfficeForm.hospitalId = "";
      this.addOfficeForm.officeName = "";
    },
    commitOffice() {
      let _this = this;
      var str = this.addOfficeForm.officeName.replace(/(^\s*)|(\s*$)/g, ""); //去除空格
      if (str == "" || str == undefined || str == null) {
        this.$notify.error({
          title: "错误",
          message: "请输入科室名称"
        });
        return;
      }
      OfficeApi.addOffice(this.addOfficeForm).then(res => {
        _this.dialogOfficeFormVisible = false;
        this.$Utils.notifySuccess();
        //this.HospitalChange();
      });
    },
    addDoctor() {
      let _this = this;
      if (
        _this.reportForm.hospitalId == "" ||
        _this.reportForm.hospitalId == null
      ) {
        this.$Utils.messageError("请先选择医院");
        return;
      }
      if (
        _this.reportForm.officeId == "" ||
        _this.reportForm.officeId == null
      ) {
        this.$Utils.messageError("请先选择科室");
        return;
      }
      this.dialogDoctorFormVisible = true;
      _this.addDoctorForm.hospitalId = _this.reportForm.hospitalId;
      _this.addDoctorForm.officeId = _this.reportForm.officeId;
    },
    dismissDoctor() {
      this.dialogDoctorFormVisible = false;
      this.addDoctorForm.hospitalId = "";
      this.addDoctorForm.officeId = "";
      this.addDoctorForm.doctorName = "";
      this.addDoctorForm.doctorTitle = "";
    },
    commitDoctor() {
      let _this = this;
      var str = this.addDoctorForm.doctorName.replace(/(^\s*)|(\s*$)/g, ""); //去除空格
      if (str == "" || str == undefined || str == null) {
        this.$Utils.messageError("请输入医生姓名");
        return;
      }
      var strTitle = this.addDoctorForm.doctorTitle.replace(
        /(^\s*)|(\s*$)/g,
        ""
      ); //去除空格
      if (strTitle == "" || strTitle == undefined || strTitle == null) {
        this.addDoctorForm.doctorTitle = "无";
      }
      DoctorApi.addDoctor(this.addDoctorForm).then(res => {
        _this.dialogDoctorFormVisible = false;
        this.$Utils.notifySuccess();
        //this.officeChange();
      });
    },
    sureReportClick() {
      this.$refs.reportForm.validate(valid => {
        if (valid) {
          // 条码号判断
          if (this.oldReportInfo.codeType === 1) {
            if (
              !this.reportForm.myReportCode ||
              this.reportForm.myReportCode === ""
            ) {
              this.$message.error("请输入平台条码号");
              return;
            }
          }
          if (this.oldReportInfo.codeType === 2) {
            if (
              !this.reportForm.insReportCode ||
              this.reportForm.insReportCode === ""
            ) {
              this.$message.error("请输入医检条码号");
              return;
            }
            /*if (this.reportForm.insReportCode === this.oldReportInfo.insReportCode
              && this.reportForm.institutionId !== this.oldReportInfo.institutionId){
              this.$message.error('相同医检条码号，不能修改医检机构')
              return
            }*/
          }

          // 收样员判断
          if (!this.reportForm.syUserId && this.reportForm.syUserIdTwo) {
            this.$message.error("请选择一级收样员");
            return;
          }
          let syList = this.reportForm.syUserIdList;
          if (syList && syList.length > 0) {
            if (!this.reportForm.syUserId) {
              this.$message.error("请选择一级收样员");
              return;
            }
            if (!this.reportForm.syUserIdTwo) {
              this.$message.error("请选择二级收样员");
              return;
            }
            let syListTwo = syList.filter(item => {
              return item.userId != null;
            });
            if (syList.length !== syListTwo.length) {
              this.$message.error("请选择多级收样员");
              return;
            }
          }
          this.$confirm(
            message.MSG_CONFIRM_SAVE_ALTER,
            message.TITLE_PROMPT,
            {}
          ).then(() => {
            if (this.reportForm.reportId) {
              //debugger
              //----------------------------修改报告单，引起条码号变更逻辑如下-----------------------------
              //3.根据平台/医检条码号生成的编号，修改各自的情况下，根据新增逻辑创建新的编号，旧的删除
              if (this.oldReportInfo.codeType == 1) {
                if (
                  this.reportForm.myReportCode !=
                  this.oldReportInfo.myReportCode
                ) {
                  // 判断是否同号
                  this.addBeforeCheck();
                } else {
                  this.alterReport();
                }
              } else if (this.oldReportInfo.codeType == 2) {
                if (
                  this.reportForm.insReportCode !=
                    this.oldReportInfo.insReportCode ||
                  this.reportForm.institutionId !=
                    this.oldReportInfo.institutionId
                ) {
                  // 判断是否同号
                  this.addBeforeCheck();
                } else {
                  this.alterReport();
                }
              }
            } else {
              return;
            }
          });
        }
      });
    },
    addBeforeCheck() {
      let _this = this;
      let reportFormData = {
        myReportCode: _this.reportForm.myReportCode,
        itemName: _this.reportForm.itemName,
        hospitalId: _this.reportForm.hospitalId,
        officeId: _this.reportForm.officeId,
        doctorId: _this.reportForm.doctorId,
        institutionId: _this.reportForm.institutionId,
        insReportCode: _this.reportForm.insReportCode,
        patientName: _this.reportForm.patientName,
        patientSex: _this.reportForm.patientSex,
        patientAge: _this.reportForm.patientAge
      };
      ReportApi.checkReportAdd(reportFormData).then(res => {
        if (res.data.reportList && res.data.reportList.length > 0) {
          _this.sameReportList = res.data.reportList;
          this.$refs.sameReportCheck.handleCreate();
        } else {
          this.alterReport();
        }
      });
    },
    addReportSure() {
      this.reportForm.codeFlag = 2;
      this.alterReport();
    },
    alterReport() {
      let _this = this;
      let reportFormData = {
        reportId: _this.reportForm.reportId,
        codeFlag: _this.reportForm.codeFlag,
        myReportCode: _this.reportForm.myReportCode,
        searchCode: _this.reportForm.searchCode,
        patientName: _this.reportForm.patientName,
        patientSex: _this.reportForm.patientSex,
        cardType: _this.reportForm.cardType,
        patientAge: _this.reportForm.patientAge,
        patientPhone: _this.reportForm.patientPhone,
        patientCard: _this.reportForm.patientCard,
        patientInfo: _this.reportForm.patientInfo,
        remark: _this.reportForm.remark,
        itemName: _this.reportForm.itemName,
        isRemind: parseInt(_this.reportForm.isRemind),
        hospitalId: _this.reportForm.hospitalId,
        officeId: _this.reportForm.officeId,
        doctorId: _this.reportForm.doctorId,
        institutionId: _this.reportForm.institutionId,
        insOfficeId: _this.reportForm.insOfficeId,
        insReportCode: _this.reportForm.insReportCode,
        syUserId: _this.reportForm.syUserId,
        syUserIdTwo: _this.reportForm.syUserIdTwo, //二级收样员
        syUserIdList: _this.reportForm.syUserIdList, // 第三级收样员开始的数组对象
        //reportState: _this.reportForm.reportState,
        reportBeginTime: _this.reportForm.reportBeginTime,
        reportImageList: _this.reportForm.reportImageList
      };
      ReportApi.editReport(reportFormData).then(data => {
        this.$Utils.notifySuccess();
        this.returnList();
      });
    },
    handleRemove(file, fileList) {
      if (file.response) {
        const url = file.response.url;
        this.reportForm.reportImageList.splice(
          this.reportForm.reportImageList.indexOf(url),
          1
        );
      }
    },
    handlePictureCardPreview(file) {
      //this.dialogImageUrl = file.url
      this.dialogImageUrl = "/static/img/pdfdefault.jpg";
      this.dialogVisible = true;
    },
    getData() {
      ReportApi.getReport({ reportId: this.$route.query.id }).then(res => {
        this.reportForm.reportBeginTime = res.data.reportBeginTime
        this.$Utils.bindingBean(this.reportForm, res.data);
        this.$Utils.bindingBean(this.oldReportInfo, res.data);
        this.reportForm.codeFlag = 1; // 默认一人多项
        //----------------------------修改报告单，引起条码号变更逻辑如下-----------------------------
        //1.根据医检条码号生成的编号，不允许补充/更改平台条码号
        //2.根据平台条码号生成的编号，补充/更改医检条码号，不需要修改其他，只修改医检条码号即可
        //3.根据平台/医检条码号生成的编号，修改各自的情况下，根据新增逻辑创建新的编号，旧的删除
        if (this.oldReportInfo.codeType === 2) {
          this.disabledMyReportCode = true;
        }

        //console.log(JSON.stringify(this.oldReportInfo),33333)
        // 收养员信息
        let syUserList = res.data.syUserList;
        if (syUserList && syUserList.length > 0) {
          this.syUserEnable = true;
          syUserList.forEach((item, index) => {
            if (index === 0) {
              this.reportForm.syUserId = parseInt(item.userId);
              if (item.collectState > 0) {
                this.syUserIdState = true;
              }
            } else if (index === 1) {
              this.reportForm.syUserIdTwo = parseInt(item.userId);
              if (item.collectState > 0) {
                this.syUserIdTwoState = true;
              }
            }
            if (index > 1) {
              this.reportForm.syUserIdList.push(item);
            }
          });
        }
        /*if (res.data.reportState){
          this.reportForm.reportState = parseInt(res.data.reportState)
        }*/
        if (res.data.hospitalId) {
          this.reportForm.hospitalId = parseInt(res.data.hospitalId);
        }

        if (res.data.officeId) {
          this.reportForm.officeId = parseInt(res.data.officeId);
        }

        if (res.data.doctorId) {
          this.reportForm.doctorId = parseInt(res.data.doctorId);
        }

        if (res.data.institutionId) {
          this.reportForm.institutionId = parseInt(res.data.institutionId);
        }
        if (res.data.insOfficeId) {
          this.reportForm.insOfficeId = parseInt(res.data.insOfficeId);
        }

        if (res.data.isRemind) {
          this.reportForm.isRemind = String(res.data.isRemind);
        }
        this.reportForm.patientSex = parseInt(res.data.patientSex);
        this.reportForm.cardType = parseInt(res.data.cardType);
        // 编辑暂时只适用于补充报告单结果上传
        //this.reportForm.reportImageList = res.data.reportImageList
        this.reportForm.reportImageList = [];
        this.listHospital();
        this.listOffice();
        this.listDoctor();
        this.listInstitution();
        this.listInsOffice()
        // this.institutionChange();
        this.listSyUser();
      });
    },
    returnList() {
      this.$store.dispatch("delView", this.$route);
      this.$router.push({ path: "/hsReport/hsReportManagement" });
    }
  },
  mounted() {
    this.timeDefaultShow = new Date();
    this.init();
  }
};
</script>
<style lang="scss" src="@/assets/css/views/add.scss" scope></style>
